package com.web.pay.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.CsvFormatStrategy;
import com.orhanobut.logger.DiskLogAdapter;
import com.orhanobut.logger.FormatStrategy;
import com.orhanobut.logger.Logger;
import com.orhanobut.logger.PrettyFormatStrategy;
import com.web.pay.R;
import com.web.pay.AppConst;
import com.web.pay.HttpJsonResponse;
import com.web.pay.MainActivity;
import com.web.pay.util.AppUtil;
import com.web.pay.util.DBManager;
import com.web.pay.util.LogUtil;
import com.web.pay.util.RequestData;
import com.web.pay.util.RequestUtils;
import com.web.pay.util.StringUtils;
import com.web.pay.util.ToastUtil;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;

public class InitActivity extends AppCompatActivity {
    private DBManager dbm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_init);

        Logger.addLogAdapter(new DiskLogAdapter());
        FormatStrategy formatStrategy2 = PrettyFormatStrategy.newBuilder()
                .tag("pay_app")   // (Optional) Global tag for every log. Default PRETTY_LOGGER
                .build();
        Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy2));


        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        InitNoticeParam();
        String url = AppConst.Pay_Login_Url;//dbm.getConfig(AppConst.KeyUKFNoticeUrl);
        if(!StringUtils.isEmpty(url)){
            EditText editText = findViewById(R.id.edit_apiurl);
            editText.setText(url);
        }
        String appid = ""+AppConst.AppId;
        if(!StringUtils.isEmpty(appid)){
            EditText editText = findViewById(R.id.edit_appid);
            editText.setText(appid);
        }
        Button btn_ok = findViewById(R.id.button_ok);
        btn_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InitActivity.this.onOkClick();
            }
        });
        Button btn_setting = findViewById(R.id.button_custom);
        btn_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InitActivity.this.toCustomSetting();
            }
        });
    }

    private void onOkClick(){
       EditText etx = findViewById(R.id.edit_apiurl);
        final String url = etx.getText().toString();
        if(StringUtils.isEmpty(url)){
            ToastUtil.show(this,"地址为空");
            return;
        }
        AppConst.NoticeUrl = url;
        dbm.setConfig(AppConst.KeyUKFNoticeUrl,url);
        String uniqueId = AppUtil.getUniqueId(this);
        String appid = dbm.getConfig(AppConst.KeyUKFNoticeAppId);
        String token = dbm.getConfig(AppConst.KeyUKFNoticeToken);
        RequestData post = RequestData.newInstance(AppConst.NetTypeLogin);
        try {
            post.put("device_id",uniqueId);
            if(!StringUtils.isEmpty(appid)){
                post.put(AppConst.KeyAppId,appid);
                post.put(AppConst.KeyToken,token);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        AppConst.NoticeUrl = url;
        RequestUtils.post(url, post, new HttpJsonResponse() {
            protected void onJsonResponse(JSONObject jsonObject) {
//                try {
//                    if(jsonObject.has(AppConst.KeyAppId)){
//                        AppConst.AppId = jsonObject.getInt(AppConst.KeyAppId);
//                        dbm.setConfig(AppConst.KeyUKFNoticeAppId,""+AppConst.AppId);
//                        LogUtil.e("appid:"+AppConst.AppId);
//                    }
//                    if(jsonObject.has(AppConst.KeyToken)){
//                        AppConst.Token = jsonObject.getString(AppConst.KeyToken);
//                        dbm.setConfig(AppConst.KeyUKFNoticeToken,AppConst.Token);
//                    }
//                    if(jsonObject.has(AppConst.KeySecret)){
//                        AppConst.Secret = jsonObject.getString(AppConst.KeySecret);
//                        dbm.setConfig(AppConst.KeyUKFNoticeSecret,AppConst.Secret);
//                    }
//                } catch (JSONException e) {
//                    LogUtil.e("login error",e);
//                }
//                startActivity(new Intent(InitActivity.this,MainActivity.class));
//                InitActivity.this.finish();

                try {
                    if(jsonObject.getInt("status") == 0) {
                        //成功了
                        startActivity(new Intent(InitActivity.this,MainActivity.class));
                        InitActivity.this.finish();
                        return;
                    }
                } catch (JSONException e) {
                    LogUtil.e("login error",e);
                    ToastUtil.show(getApplicationContext(),e.toString());
                }
                ToastUtil.show(getApplicationContext(),"登录失败--" + url);
            }
        });

    }

    private void InitNoticeParam() {
        dbm = new DBManager(this);
        AppConst.InitParams(dbm);
    }

    private void toCustomSetting(){
        startActivity(new Intent(this,SettingActivity.class));
        this.finish();
    }

}
